package cn.net.sinodata.system.controller.system;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.net.sinodata.common.log.annotation.Log;
import cn.net.sinodata.common.web.core.BaseController;
import cn.net.sinodata.common.mybatis.core.page.PageQuery;
import cn.net.sinodata.common.core.domain.R;
import cn.net.sinodata.common.core.validate.AddGroup;
import cn.net.sinodata.common.core.validate.EditGroup;
import cn.net.sinodata.common.log.enums.BusinessType;
import cn.net.sinodata.common.excel.utils.ExcelUtil;
import cn.net.sinodata.system.domain.vo.PmMsgTemplateVo;
import cn.net.sinodata.system.domain.bo.PmMsgTemplateBo;
import cn.net.sinodata.system.service.IPmMsgTemplateService;
import cn.net.sinodata.common.mybatis.core.page.TableDataInfo;

/**
 * 交易报文模板配置
 *
 * @author yinliangyun
 * @date 2025-10-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/msgTemplate")
public class PmMsgTemplateController extends BaseController {

    private final IPmMsgTemplateService pmMsgTemplateService;

    /**
     * 查询交易报文模板配置列表
     */
    @GetMapping("/list")
    public TableDataInfo<PmMsgTemplateVo> list(PmMsgTemplateBo bo, PageQuery pageQuery) {
        return pmMsgTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出交易报文模板配置列表
     */
    @Log(title = "交易报文模板配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PmMsgTemplateBo bo, HttpServletResponse response) {
        List<PmMsgTemplateVo> list = pmMsgTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "交易报文模板配置", PmMsgTemplateVo.class, response);
    }

    /**
     * 获取交易报文模板配置详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<PmMsgTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(pmMsgTemplateService.queryById(id));
    }

    /**
     * 新增交易报文模板配置
     */
    @Log(title = "交易报文模板配置", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PmMsgTemplateBo bo) {
        return toAjax(pmMsgTemplateService.insertByBo(bo));
    }

    /**
     * 修改交易报文模板配置
     */
    @Log(title = "交易报文模板配置", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PmMsgTemplateBo bo) {
        return toAjax(pmMsgTemplateService.updateByBo(bo));
    }

    /**
     * 删除交易报文模板配置
     *
     * @param ids 主键串
     */
    @Log(title = "交易报文模板配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(pmMsgTemplateService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 根据模板编号删除交易报文模板配置
     *
     * @param msgTemplateCode 模板编号
     */
    @DeleteMapping("/msgTemplateCode/{msgTemplateCode}")
    public R<Void> removeByMsgTemplateCode(@NotEmpty(message = "报文编号不能为空")
                                           @PathVariable String[] msgTemplateCode) {
        return toAjax(pmMsgTemplateService.deleteByMsgTemplateCode(msgTemplateCode));
    }

}
